package com.huike.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.service.TbBusinessTrackRecordService;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.clues.mapper.SysDataMapper;
import com.huike.clues.service.ISysDataService;
import com.huike.common.core.domain.entity.SysDictData;
import com.huike.common.core.domain.entity.SysDictDataDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @author 93238
* @description 针对表【tb_business_track_record(商机跟进记录)】的数据库操作Service实现
* @createDate 2023-10-12 06:08:37
*/
@Service
@RequiredArgsConstructor
public class TbBusinessTrackRecordServiceImpl extends ServiceImpl<TbBusinessTrackRecordMapper, TbBusinessTrackRecord>
    implements TbBusinessTrackRecordService{

    private final TbBusinessTrackRecordMapper businessTrackRecordMapper;

    private final ISysDataService sysDataService;

    @Override
    public List<TbBusinessTrackRecord> getList(Long businessId) {
        LambdaQueryWrapper<TbBusinessTrackRecord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TbBusinessTrackRecord::getBusinessId , businessId);
        List<TbBusinessTrackRecord> tbBusinessTrackRecords = businessTrackRecordMapper.selectList(lambdaQueryWrapper);
        if (tbBusinessTrackRecords.size() == 0 || tbBusinessTrackRecords == null){
            return null;
        }

        tbBusinessTrackRecords.forEach(tbBusinessTrackRecord -> {
            List<String> list = new ArrayList<>();
            String keyItems = tbBusinessTrackRecord.getKeyItems();
            String[] splits = keyItems.split(",");

            for (String split : splits) {
                LambdaQueryWrapper<SysDictData> lambdaQueryWrapper2 = new LambdaQueryWrapper();
                lambdaQueryWrapper2.eq(SysDictData::getDictType , "communication_point").eq(SysDictData::getDictValue , split);
                SysDictData one = sysDataService.getOne(lambdaQueryWrapper2);
                list.add(one.getDictLabel());
            }
            tbBusinessTrackRecord.setKeys(list);

        });
        return tbBusinessTrackRecords;
    }
}




